<?php
/**
 * Created by PhpStorm.
 * User: xuewl
 * Date: 2017/12/12
 * Time: 11:40
 */

namespace app\api\controller\v1\goods;
use app\api\controller\v1\Init;

class Order extends Init
{

    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('goods/goods_order', 'service');
    }

    /**
     * 商品兑换订单创建
     * @return \think\Response
     */

    public function create(){
        $goods_id = (int) $this->params['goods_id'];
        $user_id = (int) $this->user['id'];
        $delivery = (array) $this->params['delivery'];
        $result = $this->model->create($goods_id, $user_id, $delivery);
        if($result === false) {
            return $this->response($this->model->getError(), [], -9998);
        }
        return $this->response(lang('Operation completed'), $result);
    }


    /**
     * 获取订单列表
     * @return \think\Response
     */
    public function items() {
        $maps = [];
        $maps['uid'] = $this->user['id'];
        if(isset($this->params['goods_type'])){
            $maps['goods_type'] = $this->params['goods_type'];
        }
        if(isset($this->params['stage'])) {
            $maps['stage'] = (int) $this->params['stage'];
        }

        if(isset($this->params['status'])) {
            $maps['status'] = (int) $this->params['status'];
        }
        $order = $this->params['order'];
        $extra = [];
        $adds = [];
        $result = $this->model->lists($maps, $this->limit, $this->page, $order, true, $extra, $adds);
        return $this->response(lang('Operation completed'), $result);
    }

    /**
     * 获取订单详情
     * @return \think\Response
     */
    public function item() {
        $maps = ['order_no' => $this->params['order_no']];
        $resutl = $this->model->where($maps)->find();
        if(!$resutl) {
            return $this->response('订单不存在', [], -20002);
        }
        $resutl->stock_ticket_info;
        return $this->response('订单信息获取成功', $resutl);
    }

    /**
     * 获取统计情况
     */
    public function count() {
        $maps = ['uid' => $this->user['id']];
        $maps['status'] = ['gt', -1];
        $maps['stage'] = ['in', [1, 2]];
        //$result = $this->model->where($maps)->count();
        $result = \think\Db::name("goods_order")->where($maps)->field('stage, count(*) AS count')->group("stage")->select();
        $result = \fast\ArrayHelper::map($result, 'stage', 'count');
        return $this->response('', $result);
    }


}
